package com.wenhai.leetcode.linkedList;

import java.util.HashMap;
import java.util.Map;

/**
 *https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
 *
 * @author wenhai
 * @date   2021/3/26
 */
public class _83删除排序链表中的重复元素 {

    public ListNode deleteDuplicates(ListNode head) {
        Map<Integer,Object> container = new HashMap<>();
        ListNode prev = head;
        ListNode cur = head;
        while (cur != null) {
            Integer val = cur.val;
            if (container.get(val) != null) {
                prev.next = cur.next;
            } else {
                container.put(val, val);
                prev = cur;
            }
                cur = cur.next;

        }
        return head;

    }

    public ListNode deleteDuplicates2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        } else {
            if (head.next.val == head.val) {
                ListNode node = head.next;
                head.next = head.next.next;
                node.next = null;
                deleteDuplicates(head);
            } else {
                deleteDuplicates(head.next);
            }
            return head;
        }
    }


     public static class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1, new ListNode(1, new ListNode(2)));
        new _83删除排序链表中的重复元素().deleteDuplicates(listNode);


    }

}
